package controllers.wechatlab;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lab.jiang.Config;
import org.lab.libsvm.driver.svm_scale;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.bjtu.classification.ClassificationResult;
import cn.edu.bjtu.constant.Constant;
import cn.edu.bjtu.entity.Command;
import cn.edu.bjtu.entity.ExecuteContext;
import cn.edu.bjtu.entity.ExecuteResult;
import cn.edu.bjtu.entity.LibSVMHolderContext;
import cn.edu.bjtu.entity.ViewRenderExecuteResult;
import cn.edu.bjtu.entity.impl.CommandBuilder;
import cn.edu.bjtu.entity.impl.ExecuteContextManager;
import cn.edu.bjtu.entity.impl.GeneralCommand;
import pojo.ResCode;

@Controller
@RequestMapping("admin/svm")
public class LibSvmModelController extends WechatBaseController {
	LibSVMHolderContext lshc = new LibSVMHolderContext();
	
	@ResponseBody
	public void  svmScale (HttpServletRequest  req, HttpServletResponse res) throws IOException{
		res.getWriter().println("begin scale train file");
		System.out.println(req.getParameter("l"));
		System.out.println(req.getParameter("u"));
		String argvTrain[]={"-l",req.getParameter("l"),"-u",req.getParameter("u"),"-f",Config.Train_File_scale ,Config.Train_File};
		svm_scale.main(argvTrain);
		res.getWriter().println("begin scale Test file");		
		String argvTest[]={"-l",req.getParameter("l"),"-u",req.getParameter("u"),"-f",Config.Test_File_scale ,Config.Test_File};
		svm_scale.main(argvTest);
	}
	@RequestMapping("/train.do")
	@ResponseBody
	public void  svmModel (HttpServletRequest  req, HttpServletResponse res) throws IOException{
		Command c = new GeneralCommand();
		//LibSVMExecuteContext
		//LibSVMClssifyExecuteContext
		ExecuteContext svmBuildCtx = ExecuteContextManager.buildContext(req);
//		ExecuteContextManager.getSvmbuilder().
		ExecuteResult<LibSVMHolderContext> er = c.execute(svmBuildCtx);
		LibSVMHolderContext svmCtx = er.javaClassResult();
		res.getWriter().println(svmCtx.getModelString());
	}
	
	@ResponseBody
	@RequestMapping("/predict.do")
	public String svmpredict (HttpServletRequest  req, HttpServletResponse res) throws IOException{		
		List<String> classname = new ArrayList<String>();
		classname.add("math");
		classname.add("computer");
		classname.add("literature");
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		map.put("result", classname);
		return jsonDump(map);
		/*ExecuteContext ec = ExecuteContextManager.buildContext(req);
		Command command = CommandBuilder.build(req);
		ExecuteResult<List<ClassificationResult>> er = command.execute(ec);
		if (er instanceof ViewRenderExecuteResult) {
			ViewRenderExecuteResult<List<ClassificationResult>> x = (ViewRenderExecuteResult) er;
			List<ClassificationResult> l = (List<ClassificationResult>) x.getResultMapForRender().get(Constant.COMMAND_RESULT);
			//目前只有对一个文档分类的,所以,l的长度是1
			System.out.println(Arrays.toString(l.get(0).getDistribution()));
			return renderView("admin/classificationResult", null).addObject("result", l.get(0));
		}else{
			return renderView("admin/viewTagRendered", null).addObject(Constant.JSP_MAP_RENDER_ATTRIBUTE, er);
		}*/
	
	}

}
